package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("EchartController")
public class EchartController extends CmiEmaController {

	@RequestMapping("/listEcharts")
	public ModelAndView listEcharts(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = echartPaginationService.updatePagination(request, pagination);
		Set<Echart> echarts = echartPaginationService.getPaginationEcharts(pagination);

		mav.addObject("echarts", echarts);
		mav.addObject("entityName", "echart");
		mav.addObject("pagination", pagination);
		mav.setViewName("echart/listEcharts.jsp");
		return mav;
	}

	public String listEcharts() {
		return "redirect:/listEcharts";
	}

	@RequestMapping("/viewEchart")
	public ModelAndView viewEchart(@RequestParam Integer echartId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("echart", echartDAO.findEchartByPrimaryKey(echartId));
		mav.setViewName("echart/viewEchart.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEcharts")
	public ModelAndView confirmDeleteEcharts(
			@RequestParam(value = "deleteEchartsIds", required = true) Set<Integer> deleteEchartsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Echart> deleteEcharts = echartDAO.findEchartsByIds(deleteEchartsIds);
		Set<Echart> referencedEcharts = echartService.loadReferenceEcharts(deleteEcharts);
		boolean hasReferenced;
		if (referencedEcharts.size() == 0) {
			mav.addObject("deleteEntitys", deleteEcharts);
			hasReferenced = false;
		} else {
			deleteEcharts.removeAll(referencedEcharts);
			mav.addObject("referencedEntitys", referencedEcharts);
			mav.addObject("deleteEntitys", deleteEcharts);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Echart");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEcharts");
		return mav;
	}

	@RequestMapping("/deleteEchart")
	public String deleteEchart(@RequestParam Integer echartId) {
		Echart echart = echartDAO.findEchartByPrimaryKey(echartId);
		echartService.deleteEchart(echart);
		return "forward:/listEcharts";
	}

	@RequestMapping("/confirmDeleteEchart")
	public ModelAndView confirmDeleteEchart(@RequestParam Integer echartId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("echart", echartDAO.findEchartByPrimaryKey(echartId));

		mav.setViewName("echart/deleteEchart.jsp");

		return mav;
	}

	@RequestMapping("/deleteEcharts")
	public String deleteEcharts(
			@RequestParam(value = "deleteEchartsId", required = true) Set<Integer> deleteEchartsId) {
		echartService.deleteEcharts(deleteEchartsId);
		return "forward:/listEcharts";
	}

	@RequestMapping("/editEchart")
	public ModelAndView editEchart(@RequestParam Integer echartId) {

		ModelAndView mav = new ModelAndView();

		Echart echart = echartDAO.findEchartByPrimaryKey(echartId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("echart", echart);

		// 把所有的引用echart对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(echartService.loadEchartObjectReferencedSet(echart));

		// 将编辑页面echart添加到mav中
		mav.setViewName("echart/editEchart.jsp");
		return mav;
	}

	@RequestMapping("/newEchart")
	public ModelAndView newEchart() {
		ModelAndView mav = new ModelAndView();
		Echart echart = new Echart();

		mav.addObject("echart", echart);

		// 把所有的引用echart对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(echartService.loadEchartObjectReferencedSet(echart));

		mav.addObject("newFlag", true);
		mav.setViewName("echart/editEchart.jsp");

		return mav;
	}

	@RequestMapping("/saveEchart")
	public ModelAndView saveEchart(@ModelAttribute Echart echart) {

		ModelAndView mav = new ModelAndView();

		Echart existingEchart = CmiHibernateProxyUtil
				.deproxy(echartDAO.findEchartByPhysicalPrimaryKey(echart.getName()));

		// 新建记录
		if (echart.getId() == null) {
			if (echart.equals(existingEchart)) {

				mav.addAllObjects(echartService.loadEchartClassReferencedSet(echart));
				// mav = editContinue(mav, echart);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("echart/editEchart.jsp");
			} else {
				echartService.saveEchart(echart);
				mav.setViewName("forward:/listEcharts");
			}
		}
		// 编辑记录
		else {
			Echart originEchart = CmiHibernateProxyUtil.deproxy(echartDAO.findEchartByPrimaryKey(echart.getId()));
			if (!echart.equals(originEchart) && echart.equals(existingEchart)) {
				mav.addAllObjects(echartService.loadEchartClassReferencedSet(echart));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("echart/editEchart.jsp");
			} else {
				echartService.saveEchart(echart);
				mav.setViewName("forward:/listEcharts");
			}
		}
		return mav;
	}

	@RequestMapping("/echartController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
